from fastapi import Security, Body, Query

from apps import cruds, logic
from apps.api.v1.user_api import user_router
from apps.models import User
from apps.schemas.user_schema import CreateUserSchema, UserSchema, GetUserListSchema, UpdateUserSchema, \
    UserSchemaResponse
from apps.utils import get_current_user, get_is_admin, get_password_hash


@user_router.post("/create", name="创建用户")
async def create_user(
        user_obj: CreateUserSchema = Body(..., example={
            "account": "必填，账号 小于32字节",
            "password": "必填，密码 >=6位",
            "mobile": "手机号",
            "email": "邮箱"
        })
):
    """
    创建用户
    :param user_obj:
    :return:
    """
    count_ = await cruds.user_crud.count(**{"account": user_obj.account})
    if count_:
        return {
            'code': -1,
            "message": "账号已存在"
        }
    await cruds.user_crud.create(user_obj)
    return {
        'code': 0,
        "message": "成功"
    }


@user_router.get("/is_user_exist", response_model=bool, name="校验用户账号是否存在")
async def get_is_user_exist(
        account: str,
        user: User = Security(get_is_admin)
):
    """
    校验用户账号是否存在
    :param user:
    :param account:
    :return:
    """

    if await cruds.user_crud.get_one(account=account):
        return True
    else:
        return False


@user_router.delete("/remove", response_model=int, name="删除用户")
async def remove_user(
        user_id: int,
        user: User = Security(get_is_admin)
):
    """
    删除用户
    :param user:
    :param user_id:
    :return:
    """
    return await cruds.user_crud.remove(model_id=user_id)


# 更新用户信息
@user_router.post("/update/{user_id}", response_model=dict, name="更新用户信息")
async def update_user(
        user_obj: UpdateUserSchema,
        user_id: int,
        user: User = Security(get_is_admin)
):
    await cruds.user_crud.update(obj_in=user_obj, id=user_id)

    return {"code": 200}


# 重置密码
@user_router.post("/rest_password", response_model=dict, name="重置密码")
async def rest_password(
        user_id: int = Body(...),
        password: str = Body(...),
        user: User = Security(get_is_admin)
):
    password_obj = get_password_hash(password)
    await cruds.user_crud.rest_password(password=password_obj, id=user_id)

    return {
        "code": 0,
        "message": "重置密码成功"
    }


@user_router.get("/user_list", response_model=GetUserListSchema, name="分页获取用户")
async def remove_user(
        page: int = 1,
        limit: int = 10,
        keyword: str = None,
        user: User = Security(get_is_admin)
):
    """
    分页获取用户
    :param keyword:
    :param page:
    :param limit:
    :param user:
    :return:
    """
    skip = (page - 1) * limit

    if keyword:
        user_model_list = await cruds.user_crud.get_user_list(skip=skip, limit=limit, keyword=keyword)
        total = await cruds.user_crud.get_user_list_count(keyword=keyword)
    else:
        user_model_list = await cruds.user_crud.get_multi(skip=skip, limit=limit)
        total = await cruds.user_crud.count()

    return GetUserListSchema(total=total, result=user_model_list)


@user_router.get("/me", name="获取个人信息", response_model=UserSchemaResponse)
async def get_user_info(
        current_user: User = Security(get_current_user),
):
    return {
        'code': 0,
        "message": "成功",
        "data": current_user
    }


@user_router.post('/logout', name='退出登录')
async def user_logout(
        current_user: User = Security(get_current_user),

):
    return {
        'code': 0,
        "message": current_user
    }


@user_router.post('/updatepassword', name='修改密码')
async def update_password(
        oldpassword: str = Body(...),
        password: str = Body(...),
        repassword: str = Body(...),
        current_user: User = Security(get_current_user),
):
    oldpassword = get_password_hash(oldpassword)
    if oldpassword != current_user.password:
        return {
            'code': -1,
            "message": "旧密码不正确"
        }
    else:
        current_user.password = get_password_hash(password)
        await current_user.save()
        return {
            'code': 0,
            "message": "修改成功"
        }

